package com.common.base.utils

import android.content.Context
import android.content.SharedPreferences
import android.os.Parcelable
import com.common.base.app.extras.getContext
import com.common.base.app.extras.getDouble
import com.common.base.app.extras.getParcelable
import com.common.base.app.extras.putDouble
import com.common.base.app.extras.putParcelable

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：26/4/2023
 *
 * 描述：共享存储工具
 *
 * 修订历史：
 *
 */
object SPUtil {

//    private val APP_NAME = getContext().packageName.toString()
    private val APP_NAME = "default_sp_file_name"

    /**
     * 获取共享存储
     *
     * @param
     * @return
     */
    fun instance(): SharedPreferences {
        return getContext().getSharedPreferences(APP_NAME, Context.MODE_PRIVATE)
    }

    /**
     * 存一个boolean值
     *
     * @param key
     * @param flag
     */
    fun save(key: String, flag: Boolean) {
        val sp = instance()
        sp.edit().putBoolean(key, flag).commit()
    }

    /**
     * 存String值
     *
     * @param key
     * @param value
     */
    fun save(key: String, value: String?) {
        val sp = instance()
        sp.edit().putString(key, value).commit()
    }

    /**
     * 存Long值
     *
     * @param key
     * @param value
     */
    fun save(key: String, defValue: Long = 0) {
        val sp = instance()
        sp.edit().putLong(key, defValue).commit()
    }

    /**
     * 存Float值
     *
     * @param key
     * @param value
     */
    fun save(key: String, defValue: Float = 0f) {
        val sp = instance()
        sp.edit().putFloat(key, defValue).commit()
    }

    /**
     * 存Double值
     *
     * @param key
     * @param value
     */
    fun save(key: String, defValue: Double = 0.00) {
        val sp = instance()
        sp.edit().putDouble(key, defValue).commit()
    }

    /**
     * 存Parcelable
     *
     * @param key
     * @param value
     */
    fun save(key: String, value: Parcelable) {
        val sp = instance()
        sp.edit().putParcelable(key, value).commit()
    }

    /**
     * 获取String值
     *
     * @param key
     * @return 默认为null
     */
    fun getString(key: String): String {
        return instance().getString(key, "")?:""
    }

    /**
     * 获取String值
     *
     * @param key
     * @return 默认为null
     */
    fun getString(key: String, defValue: String): String? {
        return instance().getString(key, defValue)
    }

    /**
     * 获取boolean值
     *
     * @param key
     * @return 默认为false
     */
    fun getBoolean(key: String): Boolean {
        return getBoolean(key, false)
    }

    /**
     * 获取long值
     *
     * @param key
     * @return 默认为false
     */
    fun getLong(key: String, defValue: Long = 0): Long {
        return instance().getLong(key, defValue)
    }

    /**
     * 获取Float值
     *
     * @param key
     * @return 默认为0
     */
    fun getFloat(key: String, defValue: Float = 0f): Float {
        return instance().getFloat(key, defValue)
    }

    /**
     * 获取Double值
     *
     * @param key
     * @return 默认为0
     */
    fun getDouble(key: String, defValue: Double = 0.00): Double {
        return instance().getDouble(key, defValue)
    }

    /**
     * 获取long值
     *
     * @param key
     * @return 默认为false
     */
    fun saveLong(key: String, value: Long) {
        instance().edit().putLong(key, value).commit()
    }

    /**
     * 获取boolean值
     *
     * @param key
     * @return
     */
    fun getBoolean(key: String, defValue: Boolean): Boolean {
        return instance().getBoolean(key, defValue)
    }

    /**
     * 获取int值
     *
     * @param key
     * @return 默认为false
     */
    fun save(key: String, value: Int) {
        val sp = instance()
        sp.edit().putInt(key, value).commit()
    }

    /**
     * 获取int值
     *
     * @param key
     * @return 默认为false
     */
    fun getInt(key: String): Int {
        return instance().getInt(key, 0)
    }

    /**
     * 获取int值
     *
     * @param key
     * @return 默认为false
     */
    fun getInt(key: String, defValue: Int): Int {
        return instance().getInt(key, defValue)
    }

    inline fun <reified T : Parcelable> getParcelable(key: String): T? {
        return instance().getParcelable(key)
    }

    /**
     *
     */
    fun remove(key: String) {
        instance().edit().remove(key).commit()
    }
}
